<script setup lang="jsx">
import { computed, reactive, ref } from 'vue';
import { useElementSize } from '@vueuse/core';
import { DeleteOutlined, DownloadOutlined, EditOutlined, PlusOutlined } from '@ant-design/icons-vue';
import useResetTableRef from '@/hooks/common/resetTableRef';
import useSearchData from '@/hooks/common/searchData';
import { vcpget, vcppost } from '@/service/api/modules/erp';
const [searchParams, resetSearchParams] = useResetTableRef({
  pageNum: 1,
  pageSize: 200,
  techId: null,
  moduleNo: null,
  lotNo: null
});
const tableRef = ref(null);
const tableRefs = ref(null);
const searchData = useSearchData(searchParams);
const moduleNoList = ref([]);
const techIdNoList = ref([]);
const searchFormList = [
  {
    type: 'input',
    key: 'lotNo',
    label: 'LOT号',
    span: 24,
    md: 12,
    lg: 10,
    xl: 5,
    xxl: 5,
    placeholder: '请输入LOT号',
    showTime: true
  },
  {
    type: 'input',
    key: 'moduleNo',
    label: '生产型号',
    span: 24,
    md: 12,
    lg: 10,
    xl: 5,
    xxl: 5,
    placeholder: '请输入生产型号',
    showTime: true
  },
  {
    type: 'select',
    key: 'dept',
    selectOptions: moduleNoList,
    label: '部门',
    span: 24,
    md: 12,
    lg: 10,
    xl: 8,
    xxl: 6,
    placeholder: '请选择部门',
    showTime: true,

    change() {
      getTechId();
    }
  },
  {
    type: 'select',
    key: 'techId',
    selectOptions: techIdNoList,
    label: '工艺',
    span: 24,
    md: 12,
    lg: 10,
    xl: 8,
    xxl: 6,
    placeholder: '请选择工艺',
    showTime: true
  }
];
const { height } = useElementSize(tableRef);
const footerHeight = computed(() => {
  return `${height.value + 20}px`;
});
const pagination = reactive({
  current: 1,
  pageSize: 25,
  showSizeChanger: true,
  pageSizeOptions: ['50', '100', '200', '300', '500'],
  showQuickJumper: true,
  showTotal: total => {
    return `共${total}条`;
  },
  total: 0,
  onChange: async (current, size) => {
    pagination.current = current;
    pagination.pageSize = size;
    getData();
  }
});
const tableData = ref([]);
const tableDataTwo = ref([]);
const columns = ref([
  {
    key: 'index',
    dataIndex: 'index',
    title: '序号',
    width: 100,
    align: 'center',
    checked: true
  },
  {
    key: 'lotNo',
    dataIndex: 'lotNo',
    title: 'LOT号',
    sorter: true,
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'fullProdNo',
    dataIndex: 'fullProdNo',
    title: '生产型号',
    sorter: true,
    width: 160,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'techName',
    dataIndex: 'techName',
    title: '结存工艺',
    sorter: true,
    align: 'center',
    width: 160,
    checked: true,
    ellipsis: true
  },
  {
    key: 'wipUnit',
    dataIndex: 'wipUnit',
    title: '结存单位',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'wipQty',
    dataIndex: 'wipQty',
    title: '结存数量',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'UnitArea',
    dataIndex: 'UnitArea',
    title: '单位有效面积',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 140
  },
  {
    key: 'wipScrapPcs',
    dataIndex: 'wipScrapPcs',
    title: '废pcs数量',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'wipScrapSet',
    dataIndex: 'wipScrapSet',
    title: '废set数量',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'wipState',
    dataIndex: 'wipState',
    title: 'wip状态',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'dateCode',
    dataIndex: 'dateCode',
    title: '厂内周期',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'custDateCode',
    dataIndex: 'custDateCode',
    title: '客户周期',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'createBy',
    dataIndex: 'createBy',
    title: '入账员',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'createDate',
    dataIndex: 'createDate',
    title: '入账时间',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'lmTime',
    dataIndex: 'lmTime',
    title: '同步时间',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'updateBy',
    dataIndex: 'updateBy',
    title: '修改人',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  }
]);
const columns1 = ref([
  {
    key: 'index',
    dataIndex: 'index',
    title: '序号',
    width: 100,
    align: 'center',
    checked: true
  },
  {
    key: 'layer',
    dataIndex: 'layer',
    title: '层数',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'area',
    dataIndex: 'area',
    title: '结存面积(sq.ft)',
    sorter: true,
    align: 'center',
    checked: true,
    ellipsis: true
  }
]);
const formList = [
  {
    type: 'input',
    key: 'state',
    label: '状态',
    placeholder: '请选择状态',
    span: 8
  },
  {
    type: 'input',
    key: 'mktAllowed',
    label: 'mkt可投产',
    placeholder: '请选择是否mkt可投产',
    span: 8
  },
  {
    type: 'input',
    key: 'allowed',
    label: '可投产',
    placeholder: '请选择是否可投产',
    span: 8
  },
  {
    type: 'input',
    key: 'takeMaterials',
    label: '可领料',
    placeholder: '请选择是否可领料',
    span: 8
  },
  {
    type: 'input',
    key: 'consignment',
    label: '寄售',
    placeholder: '请输入寄售',
    span: 8
  },
  {
    type: 'input',
    key: 'quotationOrderNo',
    label: '报价单号',
    placeholder: '请输入报价单号',
    span: 8
  },
  {
    type: 'input',
    key: 'customerCode',
    label: '客户编码',
    placeholder: '请输入客户编码',
    span: 8
  },
  {
    type: 'input',
    key: 'customerName',
    label: '客户名称',
    placeholder: '请输入客户名称',
    span: 8
  },
  {
    type: 'input',
    key: 'terminalCustomer',
    label: '终端客户',
    placeholder: '请输入终端客户',
    span: 8
  },
  {
    type: 'input',
    key: 'company',
    label: '公司主体',
    placeholder: '请输入公司主体',
    span: 8
  },
  {
    type: 'input',
    key: 'factory',
    label: '生产厂区',
    placeholder: '请输入生产厂区',
    span: 8
  },
  {
    type: 'input',
    key: 'layer',
    label: '层数',
    placeholder: '请输入层数',
    span: 8
  },
  {
    type: 'input',
    key: 'prodNo',
    label: '生产型号',
    placeholder: '请输入生产型号',
    span: 8
  },
  {
    type: 'input',
    key: 'customerModel',
    label: '客户型号',
    placeholder: '请输入客户型号',
    span: 8
  },
  {
    type: 'input',
    key: 'materialNo',
    label: '物料号',
    placeholder: '请输入物料号',
    span: 8
  },
  {
    type: 'input',
    key: 'terminalModel',
    label: '终端型号',
    placeholder: '请输入终端型号',
    span: 8
  },
  {
    type: 'input',
    key: 'terminalVersion',
    label: '终端版本',
    placeholder: '请输入终端版本',
    span: 8
  },
  {
    type: 'input',
    key: 'sowFile',
    label: 'sow文件',
    placeholder: '请输入sow文件',
    span: 8
  },
  {
    type: 'input',
    key: 'otherAdditions',
    label: '其他附加',
    placeholder: '请输入其他附加',
    span: 8
  },
  {
    type: 'input',
    key: 'ptlNum',
    label: 'ptl数量',
    placeholder: '请输入ptl数量',
    span: 8
  },
  {
    type: 'input',
    key: 'ptlRemark',
    label: 'ptl备注',
    placeholder: '请输入ptl备注',
    span: 8
  },
  {
    type: 'input',
    key: 'ptlSquareMetre',
    label: 'ptl平米',
    placeholder: '请输入ptl平米',
    span: 8
  },
  {
    type: 'input',
    key: 'modelType',
    label: '型号类型',
    placeholder: '请输入型号类型',
    span: 8
  },
  {
    type: 'input',
    key: 'productType',
    label: '产品类型',
    placeholder: '请输入产品类型',
    span: 8
  },
  {
    type: 'input',
    key: 'surfaceTreatment',
    label: '表面处理',
    placeholder: '请输入表面处理',
    span: 8
  },
  {
    type: 'input',
    key: 'surfaceTreatment1',
    label: '表面处理1',
    placeholder: '请输入表面处理1',
    span: 8
  },
  {
    type: 'input',
    key: 'surfaceTreatment2',
    label: '表面处理2',
    placeholder: '请输入表面处理2',
    span: 8
  },
  {
    type: 'input',
    key: 'specialProcess',
    label: '特殊工艺',
    placeholder: '请输入特殊工艺',
    span: 8
  },
  {
    type: 'input',
    key: 'unit',
    label: '单位',
    placeholder: '请输入单位',
    span: 8
  },
  {
    type: 'input',
    key: 'customerShipUnit',
    label: '客户出货单位',
    placeholder: '请输入客户出货单位',
    span: 8
  },
  {
    type: 'input',
    key: 'unitWeight',
    label: '单重',
    placeholder: '请输入单重',
    span: 8
  },
  {
    type: 'input',
    key: 'unitArea',
    label: '单元面积',
    placeholder: '请输入单元面积',
    span: 8
  },
  {
    type: 'input',
    key: 'commonModel',
    label: '共同型号',
    placeholder: '请输入共同型号',
    span: 8
  },
  {
    type: 'input',
    key: 'plateThickness',
    label: '板厚',
    placeholder: '请输入板厚',
    span: 8
  },
  {
    type: 'input',
    key: 'plateMaterial',
    label: '板材',
    placeholder: '请输入板材',
    span: 8
  },
  {
    type: 'input',
    key: 'tg',
    label: 'tg',
    placeholder: '请输入tg',
    span: 8
  },
  {
    type: 'input',
    key: 'cti',
    label: 'cti',
    placeholder: '请输入cti',
    span: 8
  },
  {
    type: 'input',
    key: 'environmental',
    label: '环保料',
    placeholder: '请输入环保料',
    span: 8
  },
  {
    type: 'input',
    key: 'setToPcs',
    label: '1set=?pcs',
    placeholder: '请输入1set=?pcs',
    span: 8
  },
  {
    type: 'input',
    key: 'customerPanelAssembly',
    label: '客户拼板',
    placeholder: '请输入客户拼板',
    span: 8
  },
  {
    type: 'input',
    key: 'setL',
    label: 'set长',
    placeholder: '请输入set长',
    span: 8
  },
  {
    type: 'input',
    key: 'setW',
    label: 'set宽',
    placeholder: '请输入set宽',
    span: 8
  },
  {
    type: 'input',
    key: 'sampleApprovalTime',
    label: '样板承认时间',
    placeholder: '请输入样板承认时间',
    span: 8
  },
  {
    type: 'input',
    key: 'massProductionTime',
    label: '量产时间',
    placeholder: '请输入量产时间',
    span: 8
  },
  {
    type: 'input',
    key: 'hdi',
    label: 'hdi',
    placeholder: '请输入hdi',
    span: 8
  },
  {
    type: 'input',
    key: 'hdiStructure',
    label: 'hdi结构',
    placeholder: '请输入hdi结构',
    span: 8
  },
  {
    type: 'input',
    key: 'mechanicalBlindHole',
    label: '机械盲孔',
    placeholder: '请输入机械盲孔',
    span: 8
  },
  {
    type: 'input',
    key: 'flexibleBoard',
    label: '软板',
    placeholder: '请输入软板',
    span: 8
  },
  {
    type: 'input',
    key: 'productFeatures',
    label: '产品特征',
    placeholder: '请输入产品特征',
    span: 8
  },
  {
    type: 'input',
    key: 'wb',
    label: 'WB',
    placeholder: '请输入WB',
    span: 8
  },
  {
    type: 'input',
    key: 'safetyClimbManagement',
    label: '安全爬坡管理',
    placeholder: '请输入安全爬坡管理',
    span: 8
  },
  {
    type: 'input',
    key: 'safetyComponent',
    label: '安全件',
    placeholder: '请输入安全件',
    span: 8
  },
  {
    type: 'input',
    key: 'rpoth',
    label: '通孔塞树脂',
    placeholder: '请输入通孔塞树脂',
    span: 8
  },
  {
    type: 'input',
    key: 'scheduledScrapping',
    label: '预定报废',
    placeholder: '请输入预定报废',
    span: 8
  },
  {
    type: 'input',
    key: 'remark',
    label: '备注',
    placeholder: '请输入备注',
    span: 8
  },
  {
    type: 'input',
    key: 'createBy',
    label: '创建者',
    placeholder: '请输入创建者',
    span: 8
  }
];
const selectedRowKeys = ref([]);
const detailsVisible = ref(false);
const searchParams1 = ref({
  pageNum: 1,
  pageSize: 200,
  techId: null,
  moduleNo: null
});
const upDatas = ref('');
const addDatas = ref('');
const titles = ref('');
function getData() {
  vcpget('/erpsyncmes/wip/selectWipByModuleNoAndTechId', searchParams.value).then(({ data, error }) => {
    if (!error) {
      data.pageInfo.list.forEach((item, index) => {
        item.index = index + 1;
      });
      data.myMap.forEach((item, index) => {
        item.index = index + 1;
      });

      tableData.value = data.pageInfo.list;
      tableDataTwo.value = data.myMap;
    }
  });
}
getData();
function getModule() {
  vcpget('/erpsyncmes/wip/selectDept', '').then(({ data, error }) => {
    if (!error) {
      moduleNoList.value = data.map(item => ({
        label: item,
        value: item
      }));
    }
  });
}
function getTechId() {
  vcpget(`/erpsyncmes/wip/selectTechNameByDept?dept=${searchParams.value.dept}`, '').then(({ data, error }) => {
    if (!error) {
      techIdNoList.value = data.map(item => ({
        label: item,
        value: item
      }));
    }
  });
}
getModule();
function updata() {
  if (selectedRowKeys.value.length != 1) {
    return window.$message?.error('请先选择一条数据');
  }

  titles.value = '型号定义修改';
  upDatas.value = tableRef.value.tableData.find(person => selectedRowKeys.value[0].prodId == person.prodId);
  detailsVisible.value = true;
}
function addData() {
  titles.value = '型号定义新增';
  addDatas.value = {};
  detailsVisible.value = true;
}

function getUpdata() {
  vcppost('/jlerp/prodInfo/update', upDatas.value).then(({ data, error }) => {
    if (!error) {
      detailsVisible.value = false;
      upDatas.value = '';
      window.$message?.success('修改成功');
      selectedRowKeys.value = [];
      getData();
    }
  });
}

function deleteData() {
  if (selectedRowKeys.value.length == 0) {
    return window.$message?.error('请先选择一条数据');
  }
  window.$modal?.confirm({
    title: `是否确认删除数据?`,
    onOk: () => {
      // const selectData = tableRef.value.tableData.filter(item => selectedRowKeys.value.includes(item.prodId));
      const selectData = selectedRowKeys.value;
      vcppost('/jlerp/prodInfo/delete', selectData).then(({ data, error }) => {
        if (!error) {
          // const selectData = tableRef.value.tableData.filter(item => selectedRowKeys.value.includes(item.prodId));

          // pulseWaveformSelectiondeleteBatch(selectData).then(({ data, error }) => {
          //   if (!error) {
          selectedRowKeys.value = [];
          getData();
          //   }
          // });

          window.$message?.success('删除成功!');
        } else {
          window.$message?.error('删除失败!');
        }
      });
    },
    onCancel: () => {}
  });
}
const layerIcons = ref({
  1: 'fas fa-circle text-red',
  2: 'fas fa-circle text-red',
  3: 'fas fa-circle text-green',
  4: 'fas fa-circle text-green',
  5: 'fas fa-circle text-blue',
  6: 'fas fa-circle text-blue',
  7: 'fas fa-circle text-yellow',
  8: 'fas fa-circle text-yellow'
});
// 示例背景色映射
const layerColors = ref({
  1: 'rgba(135,206,255,0.2)',
  2: 'rgba(238,210,238,0.2)',
  3: 'rgba(135,206,255,0.2)',
  4: 'rgba(171,130,255,0.2)',
  5: 'rgba(238,210,238,0.2)',
  6: 'rgba(135,206,255,0.2)',
  7: 'rgba(238,210,238,0.2)',
  8: 'rgba(171,130,255,0.2)',
  A: 'rgba(135,206,255,0.2)',
  B: 'rgba(171,130,255,0.2)'
});
// 示例图片映射（使用URL）
const layerImages = ref({
  1: 'path/to/layer1.png',
  2: 'path/to/layer2.png',
  3: 'path/to/layer3.png',
  4: 'path/to/layer4.png',
  5: 'path/to/layer5.png',
  6: 'path/to/layer6.png',
  7: 'path/to/layer7.png',
  8: 'path/to/layer8.png'
});
function getLayerIcon(layer) {
  return layerIcons.value[layer];
}
function getCardColor(layer) {
  return layerColors.value[layer];
}
function getLayerImage(layer) {
  return layerImages.value[layer];
}

function search() {
  getData();
}

function reset() {
  resetSearchParams();
  search();
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <!-- <Search v-model:model="searchParams" @reset="search()" @search="search()" /> -->
    <CommonSearch
      v-model:searchParams="searchParams"
      :form-list="searchFormList"
      @search="search"
      @reset="reset"
    ></CommonSearch>

    <Table
      ref="tableRef"
      v-model:searchParams="searchData"
      :column="columns"
      :show-row-selection="false"
      :auto-generate-uuid="false"
      :scroll="{ y: 'auto' }"
      row-key="wipId"
      is-init-get-data
      :data-source="tableData"
    >
      <!--   url="/erpsyncmes/wip/selectWipByModuleNoAndTechId" -->
      <!--
 <template #summary>
        <STableSummaryRow>
          <STableSummaryCell :index="2" :col-span="2" class="text-center">
            <div>
              <span style="font-size: 22px; font-weight: 400">普通:32203.54</span>
            </div>
          </STableSummaryCell>
          <STableSummaryCell :index="4" class="text-center">
            <div>
              <span style="font-size: 16px; font-weight: 400">2</span>
            </div>
          </STableSummaryCell>
          <STableSummaryCell :index="6" class="text-center">
            <div>
              <span style="font-size: 16px; font-weight: 400">3</span>
            </div>
          </STableSummaryCell>
          <STableSummaryCell :index="8" class="text-center">
            <div>
              <span style="font-size: 16px; font-weight: 400">4</span>
            </div>
          </STableSummaryCell>
        </STableSummaryRow>
      </template>
-->
    </Table>
    <div class="titClass">各层别结存面积:</div>
    <div class="totClass">
      <ACard
        v-for="(item, key) in tableDataTwo"
        :key="key"
        title=""
        class="card"
        :style="{ backgroundColor: getCardColor(item.layer) }"
      >
        <div class="card-header">
          <i :class="getLayerIcon(item.layer)"></i>
          <SvgIcon local-icon="dcb" class="text-16px" />
          <span class="layer-number">层数: {{ item.layer }}</span>
        </div>
        <div class="card-content">
          <p class="area-info">结存面积(sq.ft): {{ item.area }}</p>
          <!-- <img :src="getLayerImage(item.layer)" :alt="'PCB Layer ' + item.layer" class="pcb-image" /> -->
        </div>
      </ACard>
    </div>

    <Modal
      v-model:visible="detailsVisible"
      :destroy-on-close="true"
      :centered="true"
      :title="titles"
      width="1050px"
      height="550px"
    >
      <div v-if="titles == '型号定义修改'" class="event-form">
        <Form v-model:model="upDatas" :form-list="formList"></Form>
      </div>
      <div v-else class="event-form">
        <Form v-model:model="addDatas" :form-list="formList"></Form>
      </div>
      <template #footer>
        <div class="modal-footer">
          <AButton @click="detailsVisible = false">关闭</AButton>
          <AButton v-if="titles == '型号定义修改'" @click="getUpdata">保存</AButton>
          <AButton v-else @click="getAddData">保存</AButton>
        </div>
      </template>
    </Modal>
  </div>
</template>

<style lang="scss" scoped>
.row-red-background {
  background-color: rgba(244, 56, 56, 0.7) !important; /* !important 可能需要以确保覆盖其他样式 */
}
.expired {
  color: red;
  font-weight: bold;
  font-size: larger;
}

:deep(.surely-table-wrapper) {
  &.has-footer-table {
    .ant-spin-nested-loading {
      .ant-spin-container {
        .surely-table {
          height: calc(100% - 40px - v-bind('footerHeight')) !important;
        }
      }
    }
  }
}
.titClass {
  font-weight: 600;
  font-size: 18px;
  margin-left: 10px;
}
.totClass {
  display: flex;
  height: 400px;
  flex-wrap: wrap;
  gap: 20px;
  border: solid 1px rgba(0, 0, 0, 0.1);
}

.card {
  width: 24%; /* 根据需要调整卡片宽度 */
  border-radius: 10px;
  // overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.6);
}

.card:hover {
  transform: scale(1.05);
}

.card-header {
  display: flex;
  align-items: center;
  // justify-content: space-between;
  font-size: 18px;
  font-weight: 800;
  padding: 15px;
  background-color: rgba(0, 0, 0, 0.05); /* 轻微背景色，增加层次感 */
}

.layer-number {
  font-size: 1.2em;
  margin-left: 10px; /* 与图标留出空间 */
}

.card-content {
  padding: 15px;
  // text-align: center;
}

.area-info {
  margin-bottom: 10px; /* 与图片留出空间 */
  font-weight: 700;
  font-size: 16px;
}

.pcb-image {
  max-width: 100%;
  height: auto;
  border-radius: 5px; /* 图片圆角 */
}

/* 示例：根据层数改变卡片背景色 */
.card[style*='background-color: #ffcccc;'] .layer-number {
  color: #990000;
} /* 1-2层，红色调 */
.card[style*='background-color: #ccffcc;'] .layer-number {
  color: #006600;
} /* 3-4层，绿色调 */
.card[style*='background-color: #ccccff;'] .layer-number {
  color: #000099;
} /* 5-6层，蓝色调 */
.card[style*='background-color: #ffffcc;'] .layer-number {
  color: #996600;
} /* 7-8层，黄色调 */
</style>
